package android.slc.appbase.ui.widget.wheel;

import android.content.Context;
import android.content.DialogInterface;
import android.slc.appbase.R;
import android.slc.pickerview.TimePickerBuilder;
import android.slc.pickerview.TimePickerView;
import android.slc.pickerview.adapter.ArrayWheelAdapter;
import android.slc.pickerview.listener.OnTimeSelectListener;
import android.slc.popup.SlcPopup2;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.StringUtils;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.google.android.material.bottomsheet.SlcBottomSheetAlertDialog2;

import java.util.Arrays;
import java.util.Calendar;

/**
 * @author slc
 * @date 2020-08-25 10:57
 */
public class TimeSelectUtils {
    public static void showSelectDateDialog(Context context, Calendar start, Calendar end, OnTimeSelectListener onTimeSelectListener) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.addView(layoutInflater.inflate(R.layout.item_my_time_picker_select, null));
        TimePickerView timePickerView = new TimePickerBuilder(context, onTimeSelectListener).setParentView(frameLayout).setRangDate(start, end).setDate(end).build();
        SlcPopup2.createAlertDialogOperate().setCancelable(false).show(new SlcBottomSheetAlertDialog2.Builder(context).setView(frameLayout).setPositiveButton(R.string.action_ok, (dialog, which) -> {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    timePickerView.returnData();
                    break;
            }
        }).setNegativeButton(R.string.action_cancel, null).create());
    }

    public static void showSelectDateDialog(Context context, TimePickerBuilder timePickerBuilder) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.addView(layoutInflater.inflate(R.layout.item_my_time_picker_select, null));
        TimePickerView timePickerView = timePickerBuilder.setParentView(frameLayout).build();
        SlcPopup2.createAlertDialogOperate().setCancelable(false).show(new SlcBottomSheetAlertDialog2.Builder(context).setView(frameLayout).setPositiveButton(R.string.action_ok, (dialog, which) -> {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    timePickerView.returnData();
                    break;
            }
        }).setNegativeButton(R.string.action_cancel, null).create());
    }

    public static void showSelectDateDialog(Context context, TimePickerBuilder timePickerBuilder, OnItemSelectedAnnexListener apSelectedListener) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        LinearLayout contentView = (LinearLayout) layoutInflater.inflate(R.layout.app_dialog_select_ap_date, null);
        TimePickerView timePickerView = timePickerBuilder.setParentView(contentView).build();
        WheelView wheelView = contentView.findViewById(R.id.wv_ap);
        wheelView.setTextSize(14);
        wheelView.setLineSpacingMultiplier(1.8f);
        wheelView.setCyclic(false);
        ArrayWheelAdapter<String> arrayWheelAdapter =
                new ArrayWheelAdapter<>(Arrays.asList(StringUtils.getString(R.string.app_label_am),
                        StringUtils.getString(R.string.app_label_pm)));
        wheelView.setAdapter(arrayWheelAdapter);
        wheelView.setOnItemSelectedListener(apSelectedListener);
        wheelView.setCurrentItem(apSelectedListener.getCurrentIndex());
        SlcPopup2.createAlertDialogOperate().setCancelable(false).show(new SlcBottomSheetAlertDialog2.Builder(context).setView(contentView).setPositiveButton(R.string.action_ok, (dialog, which) -> {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    timePickerView.returnData();
                    break;
            }
        }).setNegativeButton(R.string.action_cancel, null).create());
    }

    public interface OnItemSelectedAnnexListener extends OnItemSelectedListener {
        int getCurrentIndex();
    }
}
